<?php

namespace app\admin\controller\course;

use app\common\controller\Backend;
use app\common\model;
use app\common\model\Classes;
use app\common\model\CourseDetail;

/**
 * 学生课表
 *
 * @package app\admin\controller
 */
class Student extends Backend
{
    protected $client = null;
    protected $noNeedRight = ['detail'];
    protected $schoolLimit = true;

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $school_id = $this->request->request('school_id');
            $class_id = $this->request->request('class_id');


            //$students = model\Student::where('school_id', $school_id)->where('class_id', $class_id)->field('id,name')->select();
            $students = \app\common\model\ClassStudent::alias('a')
                ->join('__STUDENT__ b', 'b.id = a.student_id')
                ->where('a.class_id', $class_id)
                ->field('b.id,b.name')
                ->select();

            $this->success('', '', $students);
        }

        $result = model\School::with('classes')->whereIn('id', $this->auth->getSchoolIds())->select();
        $grades = model\Grade::all();

        $data = [];
        foreach ($result as $key => $value) {
            $data[$value['id']]['id'] = $value['id'];
            $data[$value['id']]['title'] = $value['name'];
            $data[$value['id']]['type'] = 'school';

            $temp_grades = [];
            foreach ($grades as $k => $grade) {
                $temp_grades[$grade['id']]['id'] = $grade['id'];
                $temp_grades[$grade['id']]['title'] = $grade['name'];
                $temp_grades[$grade['id']]['type'] = 'grade';
                foreach ($value['classes'] as $i => $class) {
                    if ($class['grade_id'] == $grade['id']) {
                        $temp_grades[$grade['id']]['children'][] = [
                            'id' => $class['id'],
                            'school_id' => $value['id'],
                            'title' => $class['name'],
                            'type' => 'classes',
                        ];
                    }
                }
            }

            $data[$value['id']]['children'] = array_values($temp_grades);
        }

        $this->assignconfig('admin', ['school' => array_values($data)]);

        return $this->view->fetch();
    }

    public function detail()
    {
        if ($this->request->isAjax()) {

            $school_id = $this->request->request('school_id');
            $class_id = $this->request->request('class_id');
            $student_id = $this->request->request('student_id');
            $semester_id = $this->request->request('semester_id');
            $semester_num = $this->request->request('semester_num');

            //课表
            $course_details = [];
            $list = CourseDetail::where('class_id', $class_id)
                ->where('semester_id', $semester_id)
                ->where('semester_num', $semester_num)
                ->select();

            $teach_ids = array_unique(array_column($list, 'teach_id'));
            $class_ids = array_unique(array_column($list, 'class_id'));

            // 教师任课
            $teaches = [];
            if ($teach_ids) {
                $teaches = model\Teach::alias('teach')
                    ->join('__SUBJECT__ subject', 'subject.id = teach.subject_id')
                    ->join('__TEACHER__ teacher', 'teacher.id = teach.teacher_id')
                    ->join('__USER__ user', 'user.id = teacher.user_id')
                    ->whereIn('teach.id', $teach_ids)
                    ->column('teach.id,teach.type,subject.name subject_name,user.nickname teacher_name', 'teach.id');
            }

            $num = 1;
            foreach ($teaches as $key => $value) {
                $teaches[$key]['num'] = $num > 8 ? $num % 8 : $num;
                $num++;
            }

            // 班级
            $classes = [];
            if ($class_ids) {
                $classes = Classes::alias('class')
                    ->join('__GRADE__ grade', 'grade.id = class.grade_id')
                    ->whereIn('class.id', $class_ids)
                    ->column('class.id,class.name class_name,grade.name grade_name', 'class.id');
            }

            foreach ($list as $key => $value) {
                if (isset($teaches[$value['teach_id']]) && isset($classes[$value['class_id']])) {
                    $course_details[$value['number']][$value['day']] = [
                        'class_name' => $classes[$value['class_id']]['class_name'],
                        'grade_name' => $classes[$value['class_id']]['grade_name'],
                        'subject_name' => $teaches[$value['teach_id']]['subject_name'],
                        'teacher_name' => $teaches[$value['teach_id']]['teacher_name'],
                        'num' => $teaches[$value['teach_id']]['num'],
                    ];
                }
            }

            //所有课节
            $class_days = [];
            $list = \app\common\model\ClassDay::where('school_id', $school_id)->select();
            foreach ($list as $key => $class_day) {
                if (isset($course_details[$class_day['number']]) && isset($course_details[$class_day['number']][$class_day['day']])) {
                    $temp = $course_details[$class_day['number']][$class_day['day']];
                    $temp['start_time'] = $class_day['start_time_text'];
                    $temp['end_time'] = $class_day['end_time_text'];
                    $class_days[$class_day['number']][] = $temp;
                } else {
                    $class_days[$class_day['number']][] = [];
                }
            }

            $this->success('', '', $class_days);

        }

    }

}